#ifndef _GAME_H
#define _GAME_H

#include "draw.h"
#include <math.h>

int judge(int);

void black(int, int, int (*)[14], int *, int *);
void white(int, int, int (*)[14], int *, int *);
int getIndex(int *, int *);
int getLocationIndex(int);

int win(int, int, int (*)[14]);
int x_line(int, int, int (*)[14]);
int y_line(int, int, int (*)[14]);
int xy_line(int, int, int (*)[14]);
int yx_line(int, int, int (*)[14]);
void drawqp(int (*)[14]);

// used to draw black piece
//  who  :   1 black  2 white    means  truning who to play
//  if in this trun , black win or white win ,the  winner will be it,used to show winnerpage
void black(int x, int y, int (*qp)[14], int *who, int *winner)
{
    int x1 = x;
    int y1 = y;

    int con = getIndex(&x, &y);

    if (con == -1)
    {
        return;
    }
    *who = 1;
    x1 = getLocationIndex(x1 - 150);
    y1 = getLocationIndex(y1 - 10);
    display_bmp("./bmp/game/b.bmp", x, y, 255, 255, 255);
    qp[y1][x1] = 1;

    int a = win(x1, y1, qp);
    if (a == 1)
    {
        *winner = 1;
    }
}

void white(int x, int y, int (*qp)[14], int *who, int *winner)
{
    int x1 = x;
    int y1 = y;
    int con = getIndex(&x, &y);
    if (con == -1)
    {
        return;
    }
    *who = 0;
    x1 = getLocationIndex(x1 - 150);
    y1 = getLocationIndex(y1 - 10);
    display_bmp("./bmp/game/w.bmp", x, y, 255, 255, 0);

    qp[y1][x1] = 2;

    int a = win(x1, y1, qp);
    if (a == 1)
    {
        *winner = 2;
    }
}

//  used to judge  win or not
int win(int x, int y, int (*qp)[14])
{
    // judge win or not needs from 4 direction

    // the x direction
    int a = x_line(x, y, qp);
    if (a == 1)
    {
        return a;
    }

    // the y dircetion
    int b = y_line(x, y, qp);
    if (b == 1)
    {
        return b;
    }

    // from upper right to lower left
    int c = xy_line(x, y, qp);
    if (c == 1)
    {
        return c;
    }

    // from upper left to lower right
    int d = yx_line(x, y, qp);
    if (d == 1)
    {
        return d;
    }
    return 0;
}


int yx_line(int x, int y, int (*qp)[14])
{
    int num = qp[y][x];
    int down = 0;

    int y1 = y + 1;
    int x1 = x + 1;

    while (qp[y1][x1] == num && x1 < 14 && y1 < 14)
    {

        down++;
        x1++;
        y1++;
    }

    int up = 0;
    y1 = y - 1;
    x1 = x - 1;

    while (qp[y1][x1] == num && x1 >= 0 && y1 >= 0)
    {
        up++;
        x1--;
        y1--;
    }

    int all = up + down;

    if (all >= 4)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int xy_line(int x, int y, int (*qp)[14])
{
    int num = qp[y][x];
    int down = 0;

    int y1 = y - 1;
    int x1 = x + 1;

    while (qp[y1][x1] == num && x1 < 14 && y1 >= 0)
    {

        down++;
        x1++;
        y1--;
    }

    int up = 0;
    y1 = y + 1;
    x1 = x - 1;

    while (qp[y1][x1] == num && x1 >= 0 && y1 < 14)
    {
        up++;
        x1--;
        y1++;
    }

    int all = up + down;

    if (all >= 4)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int y_line(int x, int y, int (*qp)[14])
{
    int num = qp[y][x];
    int down = 0;

    for (int i = y + 1; i < 14; i++)
    {
        if (qp[i][x] == num)
        {
            down++;
        }
        else
        {
            break;
        }
    }
    int up = 0;

    for (int i = y - 1; i >= 0; i--)
    {
        if (qp[i][x] == num)
        {
            up++;
        }
        else
        {
            break;
        }
    }

    int all = up + down;

    if (all >= 4)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}



int x_line(int x, int y, int (*qp)[14])
{
    int num = qp[y][x];
    int right = 0;

    for (int i = x + 1; i < 14; i++)
    {
        if (qp[y][i] == num)
        {
            right++;
        }
        else
        {
            break;
        }
    }

    int left = 0;

    for (int i = x - 1; i >= 0; i--)
    {
        if (qp[y][i] == num)
        {
            left++;
        }
        else
        {
            break;
        }
    }

    int all = right + left;
    if (all >= 4)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}


// according to the you click coordinate to find the near crosspoint in the board
int getIndex(int *a, int *b)
{

    int x = *a;
    int y = *b;

    x -= 150;
    y -= 10;

    x = judge(x);
    y = judge(y);

    if (x == -1 || y == -1)
    {
        return -1;
    }
    x += 150;
    y += 10;

    x -= 15;
    y -= 15;

    *a = x;
    *b = y;
    return 1;
}

// get the point in the chessbord  according to (a,b) the coordinate of the whole UI
int getLocationIndex(int location)
{

    int index = 0;

    for (int i = 0; i < 459; i += 35)
    {
        if (location == i)
        {
            return index;
        }
        if (abs(location - i) <= 15)
        {
            return index;
        }

        index++;
    }
}


//if you click point on the crosspoint or the distance is 15 pixel ,then that is it and return it
int judge(int point)
{
    for (int i = 0; i < 459; i += 35)
    {
        if (point == i)
        {
            return point;
        }
        if (abs(point - i) <= 15)
        {
            return i;
        }
    }
    return -1;
}


//no use   draw the whole chessboard only  just for test
void drawqp(int (*qp)[14])
{

    for (int i = 0; i < 14; i++)
    {
        for (int j = 0; j < 14; j++)
        {
            printf("%d ", qp[i][j]);
        }
        printf("\n");
    }
}


#endif